/**
 * 
 */
package com.tdkj.action.front.member.buyer;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.model.shop.BeanOrder;
import com.tdkj.model.shop.BeanOrderConsigneeInfo;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.model.shop.BeanShoppingCart;
import com.tdkj.model.shop.BeanShoppingCartItem;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.ServiceException;
import com.tdkj.service.shop.IBeanOrderService;
import com.tdkj.service.shop.IBeanShoppingCartService;
import com.tdkj.util.Page;
import com.tdkj.util.PublicConfing;

/**
 * @author yninfo
 */
public class ShoppingCartAction extends BaseAction {
	
	private Long itemId ;
	private Long goodId ;
	private Integer quantity;
	
	private List<BeanShoppingCartItem> items;
	
	private BeanShoppingCart shoppingCart ;
	private IBeanShoppingCartService shoppingCartService;
	private IBeanOrderService orderService;
	
	private BeanOrderConsigneeInfo consigneeInfo;
	
	private List<BeanOrderConsigneeInfo> consigneeInfos;
	private LinkedHashMap<BeanShop,List<BeanShoppingCartItem>> shopItemsGroupMap = new LinkedHashMap<BeanShop,List<BeanShoppingCartItem>>();
	
	private BeanOrder order;
	
	public String execute(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();

		shoppingCart = shoppingCartService.getByUserId(userId);
		
		for(BeanShoppingCartItem item : shoppingCart.getItems()){
			BeanShop shop = item.getGood().getShelf().getShop();
			List<BeanShoppingCartItem> items = shopItemsGroupMap.get(shop);
			if(items == null){
				items = new ArrayList<BeanShoppingCartItem>();
				shopItemsGroupMap.put(shop, items);
			}
			items.add(item);
		}
		
		consigneeInfos = shoppingCartService.getConsigneeInfosByUserId(userId);
		
		String hqlGoods = " from " + BeanGood.class.getName() + " as bean where bean.showcase!=null order by publishTime desc";
		Query queryGoods = this.createQuery(hqlGoods);
		
		Page goods = this.getBaseService().getBaseDao().findBy(queryGoods, 1, 20);
		this.getReturnInfo().put("goods", goods);
		
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		return "success";
	}

	public String addItem() throws ServiceException{
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		shoppingCartService.addItem(userId, goodId, quantity);
		
		return execute();
	}
	
	public String removeItem(){
		shoppingCartService.removeItem(itemId);
		
		return execute();
	}
	
	public String immediately() throws ServiceException{
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		shoppingCartService.addItem(userId, goodId, quantity);
		
		return confirm();
	}
	
	public String confirm(){
		execute();
		return "confirm";
	}
	
	public String createOrder(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		for(BeanShoppingCartItem item : items){
			shoppingCartService.saveOrUpdate(item, BeanShoppingCartItem.class);
		}
		if(consigneeInfo.getId() == null){
			shoppingCartService.addConsigneeInfo(userId, consigneeInfo);
		}
		
		List<BeanOrder> orders = orderService.create(userId, consigneeInfo);
		order = orders.get(0);
		
		return "orderToPay";
	}
	
	public String user(){
		return "guest";
	}
	
	public Long getGoodId() {
		return goodId;
	}

	public void setGoodId(Long goodId) {
		this.goodId = goodId;
	}

	public BeanShoppingCart getShoppingCart() {
		return shoppingCart;
	}

	public void setShoppingCart(BeanShoppingCart shoppingCart) {
		this.shoppingCart = shoppingCart;
	}

	@Resource
	public void setShoppingCartService(IBeanShoppingCartService shoppingCartService) {
		this.shoppingCartService = shoppingCartService;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public LinkedHashMap<BeanShop, List<BeanShoppingCartItem>> getShopItemsGroupMap() {
		return shopItemsGroupMap;
	}

	public void setShopItemsGroupMap(
			LinkedHashMap<BeanShop, List<BeanShoppingCartItem>> shopItemsGroupMap) {
		this.shopItemsGroupMap = shopItemsGroupMap;
	}

	@Resource
	public void setOrderService(IBeanOrderService orderService) {
		this.orderService = orderService;
	}

	public List<BeanOrderConsigneeInfo> getConsigneeInfos() {
		return consigneeInfos;
	}

	public void setConsigneeInfos(List<BeanOrderConsigneeInfo> consigneeInfos) {
		this.consigneeInfos = consigneeInfos;
	}

	public BeanOrderConsigneeInfo getConsigneeInfo() {
		return consigneeInfo;
	}

	public void setConsigneeInfo(BeanOrderConsigneeInfo consigneeInfo) {
		this.consigneeInfo = consigneeInfo;
	}

	public BeanOrder getOrder() {
		return order;
	}

	public void setOrder(BeanOrder order) {
		this.order = order;
	}

	public List<BeanShoppingCartItem> getItems() {
		return items;
	}

	public void setItems(List<BeanShoppingCartItem> items) {
		this.items = items;
	}
	
}
